27. Find the Longest Name

Find the longest name

Question:

Start Quiz:

public String findLongestName(String [] names){


    return "";
}
Solution:

Quiz hints

You should implement the function String findLongestName(String [] names) which takes an array of Strings as an input containing a list of names, and return the String that has the longest name.

To do so, try to follow these steps:

  1. The first step is to calculate and store the length of the input array, this is done using names.length; and store that in an integer variable.

  2. Then create a new String called longestName that will store the longest name in the array of names, initialize it with the first name in the array, that is names[0].

  3. Next, you should create a for loop that will compare every name in the array using names[i] against the longestName. Only replace the longestName value if the names[i] is longer .

  4. Finally, return the longestName variable as the return value of the function.